<?php
/**
 * View Nuovo Utente - LaFabbrica.org
 * 
 * @author     luigi.pastore (gigi@la-fabbrica.org)
 * @created    21-apr-2011
 * @package    LaFabbrica
 * @copyright  Copyright (c) 2011 Luigi Pastore
 */
?>
<script type="text/javascript">
//<![CDATA[
    $(document).ready(function() {
	$.validator.addMethod(
	    "passwordUnrequired",
	    function(value, element, regexp) {
		if(value == '')
		    return true;
		
		return regexp.test(value);
	    }

	);
	

	$("#userForm").validate({
	    invalidHandler: function(e, validator) {
		var errors = validator.numberOfInvalids();
		if (errors) {
		    $("li#errorLi").show();
		} else {
		    $("li#errorLi").hide();
		};
	    },
	    
	    // Assegna classe 'error' al <li> dell'elemento invalido
	    errorPlacement: function(error, element) {
		console.log(element);
		element.parent("span").parent("div").parent("li").addClass("error");
		error.appendTo( element.parent("span").parent("div"));
	    },
	    submitHandler: function(form) {
		$('#salva').attr('disabled', 'disabled');
		form.submit();
	    },
	    onfocusout: false,
	    errorElement: "p",
	    groups: {
		name: "user_firstname user_lastname",
		password: "user_password password_again"
	      },
	    rules: {
		user_firstname:{
		    required: true,
		    remote: {
			url: "/admin/utenti/check?f=name",
			type: "post",
			data: {
			  name: function() {
			    return $("#user_firstname").val();
			  }
			}
		    }
		},
		user_lastname:{
		    required: true,
		    remote: {
			url: "/admin/utenti/check?f=name",
			type: "post",
			data: {
			  name: function() {
			    return $("#user_lastname").val();
			  }
			}
		    }
		},
		user_email:{
		    required: true,
		    email: true,
		    remote: {
			url: "/admin/utenti/check?f=email",
			type: "post",
			data: {
			  user_id: function() {
			    return $("#user_id").val();
			  }
			}
		    }
		},
		user_password:{
		    passwordUnrequired: /^[0-9a-zA-Z.]{6,}$/i
		},
		password_again: {
		    equalTo: "#user_password"
		}		
	    },
	    messages: {
		user_firstname: {
		    required: "<?php echo ERR_EMPTY ?>",
		    remote: "<?php echo  sprintf( ERR_FORMAT, 'Nome' )?>"
		},
		user_lastname: {
		    required: "<?php echo ERR_EMPTY ?>",
		    remote: "<?php echo  sprintf( ERR_FORMAT, 'Cognome' )?>"
		},
		user_password: {
		    required: "<?php echo ERR_EMPTY ?>",
		    regex: "<?php echo ERR_PASSWORD ?>"
		},
		password_again: {
		    required: "<?php echo ERR_EMPTY ?>",
		    equalTo: "<?php echo ERR_PASSWORD_MATCH ?>"
		},
		user_email: {
		    required: "<?php echo ERR_EMPTY ?>",
		    email: "<?php echo ERR_EMAIL ?>",
		    remote: "<?php echo  sprintf( ERR_DUPLICATE, 'Email' )?>"
		}		
	    },
	    debug:true
	});
    });
    //]]>
</script>

<div class="info">
    <h2>Inserisci Nuovo Utente</h2>
    <div>Per inserire un nuovo post...</div>
</div>
<!-- This <div> holds alert messages to be display in the sample page. -->
<div id="alerts">
    <noscript>
    <p>
	<strong>CKEditor requires JavaScript to run</strong>. In a browser with no JavaScript
				support, like yours, you should still see the contents (HTML data) and you should
				be able to edit it normally, without a rich editor interface.
    </p>
    </noscript>
</div>
<form class="fab" name="userForm" id="userForm" action="/admin/utenti/edit/id/<?php echo$this->data['user_id']?>" method="post">
    <ul>
	<li id="errorLi" style="<?php echo$this->errorMsg ? '' : 'display:none'?>">
	    <h3 id="errorMsgLbl">Si Ã¨ verificato un problema durante l'invio del modulo.</h3>
	    <p id="errorMsg">
		Di seguito trovi evidenziati gli errori riscontrati.
	    </p>
	</li>
	
	<li class="<?php echo$this->errorMsg['user_firstname'] ? 'error' : ''?>">
	    <div class="float-left">
	    <label class="desc <?php echo$this->errorMsg['user_firstname'] ? 'error' : ''?>">Nome<span class="req">*</span></label>
	    <span class="input">
		<input class="field text required" id="user_firstname" name="user_firstname" size="20" value="<?php echo$this->data['user_firstname']?>"/>
		<label>Nome</label>
	    </span>
	    <span class="input">
		<input class="field text required" id="user_lastname" name="user_lastname" size="20" value="<?php echo$this->data['user_lastname']?>"/>
		<label>Cognome</label>
	    </span>
	    </div>
	    <p class="error" style="<?php echo$this->errorMsg['user_firstname'] ? 'display:block' : ''?>"><?php echo$this->errorMsg['user_firstname']?></p>
	</li>

	<li class="<?php echo$this->errorMsg['user_email'] ? 'error' : ''?>">
	    <label class="desc <?php echo$this->errorMsg['user_email'] ? 'error' : ''?>">Email<span class="req">*</span></label>
	    <div>
		<span>
		    <input id="user_email" name="user_email" class="field text medium" type="text" maxlength="255" value="<?php echo$this->data['user_email']?>" /> 
		</span>
	    </div>
	    <p class="error" style="<?php echo$this->errorMsg['user_email'] ? 'display:block' : ''?>"><?php echo$this->errorMsg['user_email']?></p>
	</li>	
	
	<li class="section">
	    <h3></h3>
	</li>
	
	<li class="<?php echo$this->errorMsg['user_password'] ? 'error' : ''?>">
	    <div class="float-left">
	    <label class="desc <?php echo$this->errorMsg['user_password'] ? 'error' : ''?>">Cambia Password</label>
	    <span class="input">
		<input class="field text" id="user_password" name="user_password" type="password" size="20" />
		<label>Password</label>
	    </span>
	    <span class="input">
		<input class="field text" id="password_again" name="password_again"  type="password" size="20" />
		<label>Ripeti Password</label>
	    </span>
	    </div>
	    <p class="error" style="<?php echo$this->errorMsg['user_password'] ? 'display:block' : ''?>"><?php echo$this->errorMsg['user_password']?></p>
	</li>

	<li>
	    <label class="desc">Gruppo Utente</label>
	    <div>
		<span class="input">
		    <select class="field select addr" id="user_group_id" name="user_group_id">
		    <?php foreach( $this->groups as $group ) : ?>
			<option value="<?php echo$group['group_id']?>"><?php echo$group['group_name']?></option>
		    <?php endforeach; ?>
		    </select>
		</span>
	    </div>
	</li>
	<input id="user_id" name="user_id" type="hidden" value="<?php echo$this->data['user_id']?>" />
    </ul>
    <div id="formButtons">
	<button id="salva" type="submit" name="salva" value="salva" class="button positive">
	    <img src="/images/admin/tick.png" alt="" />Salva
	</button>
    </div>    
</form>
